package com.bookmarket.controller;

import com.bookmarket.entity.Discount;
import com.bookmarket.service.CzgIDiscountService;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Chen Zhaoguang
 * @date 2018/1/2 9:30
 */
@RestController
@RequestMapping("/czgDiscount")
public class CzgDiscountController {

    @Resource
    private CzgIDiscountService czgIDiscountService;

    //优惠券
    //分页查询，模糊查询
    @RequestMapping("/queryByPage")
    public Map queryByPage(int pageNum, int pageSize, Discount discount) {
        Map map = new HashMap();
        /*ModelAndView mv = new ModelAndView();*/
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("discount", discount);
        Page page = czgIDiscountService.queryLikeDiscounts(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        /*mv.addObject("map",map);
        mv.setView(new MappingJackson2JsonView());*/
        return map;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ModelAndView addDiscount(Discount discount) throws ParseException {
        ModelAndView mv = new ModelAndView();
        Map<String, Object> cond = new HashMap<>();
        cond.put("discount", discount);
        czgIDiscountService.addDiscount(cond);
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("dis", discount);
        return mv;
    }

    @RequestMapping("/del")
    public ModelAndView del(Discount discount) {
        ModelAndView mv = new ModelAndView();
        czgIDiscountService.delDiscount(discount);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    /**
     * 查询单个优惠券的详细信息
     *
     * @return
     */
    @RequestMapping("/editp")
    public ModelAndView findUser(int id) {
        Discount rsDiscount = czgIDiscountService.getDiscount(id);
       /* Map<String,Object> rsMap = new HashMap<String,Object>();
        rsMap.put("data", rsDiscount);
        return new ModelAndView("backstage/CzgEditDiscount",rsMap);*/
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("rs",rsDiscount);
        return mv;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public ModelAndView editUser(Discount discount) {
      //  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ModelAndView mv = new ModelAndView();
        Map<String, Object> cond = new HashMap<>();
        cond.put("discount", discount);
        System.out.println(discount.getId());
        czgIDiscountService.editDiscount(cond);
        mv.setView(new MappingJackson2JsonView());
        System.out.println(discount.getStartTime());
        return mv;


    }

    public CzgIDiscountService getCzgIDiscountService() {
        return czgIDiscountService;
    }

    public void setCzgIDiscountService(CzgIDiscountService czgIDiscountService) {
        this.czgIDiscountService = czgIDiscountService;
    }
}
